<template>
	<view class="container">
		<view class="content">
			<view class="option">
				<view class="title">产品名称</view>
				<view class="con">
					<input type="text" placeholder="请输入产品名称" v-model="goodsInfo.title">
				</view>
			</view>
			<view class="option">
				<view class="title">产品分类</view>
				<view class="con" @click="typeShow = true">
					<input type="text" placeholder="请选择产品分类" disabled="" v-model="fenlei">
				</view>
			</view>
			<view class="option">
				<view class="title">菜系分类</view>
				<view class="con" @click="cuisineShow = true">
					<input type="text" placeholder="请选择菜系分类" disabled="" v-model="caixi">
				</view>
			</view>
			<view class="option">
				<view class="title">产品品牌</view>
				<view class="con" @click="brandShow = true">
					<input type="text" placeholder="请选择产品品牌" disabled="" v-model="pinpai">
				</view>
			</view>
			<view class="option">
				<view class="title">产品描述</view>
				<view class="con">
					<input type="text" placeholder="请输入产品描述" v-model="goodsInfo.description">
				</view>
			</view>
			<view class="option">
				<view class="title">产品价格</view>
				<view class="con">
					<input type="text" placeholder="请输入产品价格" v-model="goodsInfo.price">
				</view>
			</view>
			<view class="option">
				<view class="title">产品图片</view>
				<view class="con">
					<view class="img_box" v-for="(item,index) in images" :key="index" @click="lookImages(index)">
						<image :src="item.img" mode="widthFix" style="width: 100%;"></image>
						<view class="del_btn" @click.stop="delImg(index)">
							<u-icon name="trash"></u-icon>
						</view>
					</view>
					<view class="upload_box" @click="uploadImg">
						<u-icon name="plus" size="80" color="#999"></u-icon>
					</view>
				</view>
			</view>
			<view class="option">
				<view class="title">产品规格</view>
				<view class="con">
					<view class="sku_box" v-for="(item,index) in skuList" :key="index">
						<view class="boxsss">
							<view class="sku_name input_box">
								<input type="text" placeholder="规格名称" v-model="item.title">
							</view>
							<view class="sku_price input_box">
								<input type="number" placeholder="价格" v-model="item.price">
							</view>
							<view class="sku_price input_box">
								<input type="number" placeholder="库存(库存如果不限量填入-1)" v-model="item.storage">
							</view>
						</view>
						<view class="del_sku" @click="delSku(index)" v-if="skuList.length>1">删除</view>
					</view>
					<view class="add_sku" @click="addsku"><u-icon name="plus"></u-icon>新增规格</view>
				</view>
			</view>
			<view class="option">
				<view class="title">产品详情</view>
				<view class="con">
					<editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar
						show-img-resize @input="editorInput" @ready="onEditorReady">
					</editor>
				</view>
			</view>
		</view>
		<view class="submit" @click="addGoods">
			新增产品
		</view>
		<u-select v-model="typeShow" mode="mutil-column-auto" :list="goodsType" @confirm="typeSelectConfirm"
			value-name="id" label-name="title" child-name="child"></u-select>
		<u-select v-model="cuisineShow" value-name="id" label-name="title" :list="cuisineList"
			@confirm="cuisineSelectConfirm"></u-select>
		<u-select v-model="brandShow" value-name="id" label-name="title" :list="brandList"
			@confirm="brandSelectConfirm"></u-select>
	</view>
</template>

<script>
	import index from '../../uview-ui'
	export default {
		data() {
			return {
				readOnly: false,
				typeShow: false,
				cuisineShow: false,
				brandShow: false,
				skuList: [{
					title: '',
					price: '',
					storage: '',
				}],
				goodsType: [],
				cuisineList: [],
				brandList: [],
				images: [],
				caixi: '',
				fenlei: '',
				pinpai: '',
				editorCtx: null,
				goodsInfo: {
					"category": "", //分类id
					"cook_category": "", //菜系分类
					"brand": "", //品牌id
					"title": "", //商品名搜索
					"thumb": "", //图片id
					"images": [], //图片id-数组
					"description": "", //描述
					"price": "", //价格
					"content": "", //内容
					"spec": [] //规格-数组
				},
				goodsId: 0,
			}
		},
		async onLoad(opdata) {
			this.goodsId = opdata.id
			await this.getGoodsType()
			await this.getBrandList()
			await this.getCuisine()
			await this.getMygoods()
		},
		methods: {
			async getMygoods() {
				await this.$allApi.getMyGoodsDetail({
					id: this.goodsId
				}).then(res => {
					let data = res.data.data
					this.fenlei = data.categoryinfo.title
					this.pinpai = data.brandinfo.title
					this.skuList = data.speclist
					for (let i in data.images) {
						this.images.push({
							img: data.images_path[i],
							id: data.images[index]
						})
					}
					this.goodsInfo = {
						"category": data.category, //分类id
						"cook_category": data.cook_category, //菜系分类
						"brand": data.brand, //品牌id
						"title": data.title, //商品名搜索
						"thumb": data.thumb, //图片id
						"images": data.images, //图片id-数组
						"description": data.description, //描述
						"price": data.price, //价格
						"content": data.content, //内容
						"spec": data.speclist //规格-数组
					}
					this.onEditorReady()
				})
			},
			addGoods() {
				if (this.goodsInfo.title == '') return this.$u.toast('商品名不能为空')
				if (this.goodsInfo.category == '') return this.$u.toast('请选择商品分类')
				if (this.goodsInfo.cook_category == '') return this.$u.toast('请选择商品菜系')
				if (this.goodsInfo.brand == '') return this.$u.toast('请选择商品品牌')
				if (this.goodsInfo.content == '') return this.$u.toast('产品详情不能为空')
				if (this.goodsInfo.price == '') return this.$u.toast('商品价格不能为空')
				if (this.images.length == 0) return this.$u.toast('请上传商品图片')
				for (let item of this.skuList) {
					if (item.title == '') {
						console.log(item);
						return this.$u.toast('商品规格名字不能为空')
					}
					if (item.price == '') {
						return this.$u.toast('商品规格价格不能为空')
					}
					if (item.storage == '') {
						return this.$u.toast('商品规格库存不能为空')
					}
				}
				this.goodsInfo.spec = this.skuList
				for (let item of this.images) {
					this.goodsInfo.images.push(item.id)
				}
				this.goodsInfo.thumb = this.goodsInfo.images[0]
				this.$allApi.addProduct(this.goodsInfo).then(res => {
					this.$u.toast('修改商品信息成功')
					setTimeout(() => {
						this.$backTo()
					}, 1500)
				})
			},
			delSku(index) {
				this.skuList.splice(index, 1)
			},
			typeSelectConfirm(e) {
				console.log(e);
				this.goodsInfo.category = e[1].value
				this.fenlei = e[0].label + '-' + e[1].label
			},
			cuisineSelectConfirm(e) {
				console.log(e);
				this.goodsInfo.cook_category = e[0].value
				this.caixi = e[0].label
			},
			brandSelectConfirm(e) {
				console.log(e);
				this.goodsInfo.brand = e[0].value
				this.pinpai = e[0].label
			},
			async getGoodsType() {
				await this.$allApi.getGoodsType({
					type: 1,
					child: 1,
				}).then(res => {
					this.goodsType = res.data.list
					console.log(this.goodsType);
				})
			},
			async getBrandList() {
				await this.$allApi.getBrandList().then(res => {
					this.brandList = res.data.list
				})
			},
			async getCuisine() {
				await this.$allApi.getGoodsType({
					type: 2,
					child: 2,
				}).then(res => {
					this.cuisineList = res.data.list
				})
			},
			onStatusChange(e) {
				console.log(e);
			},
			onEditorReady(e) {
				// #ifdef APP-PLUS || MP-WEIXIN || H5
				setTimeout(() => {
					uni.createSelectorQuery().select('#editor').context((res) => {
						this.editorCtx = res.context
						this.editorCtx.setContents({
							html: this.goodsInfo.content
						})
					}).exec()
				}, 300)
				// #endif
				console.log(this.goodsInfo.content);
			},
			editorInput(e) {
				this.goodsInfo.content = e.detail.html
			},
			addsku() {
				this.skuList.push({
					name: '',
					price: ''
				})
			},
			delImg(index) {
				this.images.splice(index, 1)
			},
			lookImages(index) {
				uni.previewImage({
					urls: this.images,
					current: index
				})
			},
			uploadImg() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						for (let item of res.tempFiles) {
							if (res.tempFiles[0].size > item.size) {
								this.$api.total('图片大小不能超过5MB，请检查')
								return
							}
							uni.uploadFile({
								url: this.$baseUrl + 'api/upload/upload',
								filePath: item.path,
								name: 'file',
								success: (imgRes) => {
									this.images.push({
										img: JSON.parse(imgRes.data).path,
										id: JSON.parse(imgRes.data).id
									})
								}
							})
						}

					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.option {
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		min-height: 90rpx;
		border-bottom: 1px solid #ECECEC;
		padding: 20rpx 0;

		.con {
			flex: 1;
			padding-left: 20rpx;

			.del_sku {
				padding: 6rpx 10rpx;
				font-size: 24rpx;
				color: #fff;
				background: #40AE36;
				border-radius: 6rpx;
				margin-left: 20rpx;
			}

			.img_box {
				position: relative;
				width: 100%;

				.del_btn {
					position: absolute;
					top: 0;
					right: 0;
					color: #fff;
					font-size: 60rpx;
					width: 80rpx;
					height: 80rpx;
					background: rgba(0, 0, 0, .5);
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 0 0 0 50%;
				}
			}

			editor {
				border: 1px solid #eee;
				border-radius: 10rpx;
				padding: 10rpx;
			}

			.sku_box {
				flex: 1;
				font-size: 24rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 20rpx;

				.boxsss {
					flex: 1;
				}

				.input_box {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: flex-start;
					height: 60rpx;
					border-radius: 6rpx;
					background: #F9F9F9;
					margin-bottom: 10rpx;
					padding-left: 30rpx;
				}
			}

			.add_sku {
				height: 60rpx;
				border-radius: 6rpx;
				background: #F9F9F9;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #999;
				font-size: 24rpx;
			}

			.upload_box {
				width: 270rpx;
				height: 270rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 20rpx;
				border: 1px solid #eee;
			}

			input {
				flex: 1;
			}
		}

		.title {
			display: flex;
			align-items: center;
			width: 160rpx;
		}
	}

	.submit {
		width: 710rpx;
		height: 80rpx;
		border-radius: 10rpx;
		background: #40AE36;
		z-index: 99;
		left: 20rpx;
		position: fixed;
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;

	}

	.content {
		min-height: calc(100vh - 120rpx - env(safe-area-inset-bottom));
		overflow-y: scroll;
		background: #fff;
		padding: 20rpx;
		padding-top: 0;
	}

	.container {
		padding: 20rpx;
		min-height: 100vh;
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
		background: #f8f8f8;
	}
</style>